from mod_verbs import Verb
import mod_effects
import mod_gui
import mod_line
from mod_item import ItemPrototype

class Shoot(Verb):
    def __init__(self,bulletPrototype):
        Verb.__init__(self,'SHOOT','BUTTON_SHOOT',2,'coords')
        self.bulletPrototype = bulletPrototype
    
    def calculateOddsOfHit(self,actor,target):
        distance = mod_line.getDistance(actor.location[1],target)
        #odds = 0.9/((0.8*distance) + 1.2)
        odds = 0.5/((0.18*distance)+1)
        return odds
    
    def doIt(self,actor,item,target):
        actor.useAP(self.AP)
        if item:
            mod_gui.theGUI.messages.append(actor.glyph+' FIRES '+item.glyph+'.')
        else:
            mod_gui.theGUI.messages.append(actor.glyph+' SHOOTS.')
        oddsFunction = self.calculateOddsOfHit
        projectile = self.bulletPrototype.instantiate()
        
        mod_effects.fireProjectile(actor,projectile,target,oddsFunction,rotateImage=True)

class BlasterBoltImpactEffect:
    def __init__(self,power):
        self.power = power
    
    def doIt(self,blasterBolt):
        level,coords = blasterBolt.location
        blasterBolt.setLocation(None)
        entity = level.entityMap.get(coords)
        if entity:
            entity.takeDamage(self.power,'blast')

rayPrototype = ItemPrototype('RAY',grabbable=False,stopsBeforeWall=False)
rayPrototype.impactEffect = BlasterBoltImpactEffect(1)

raygunPrototype = ItemPrototype('RAYGUN',grabbable=True)
raygunPrototype.verbs.append(Shoot(rayPrototype))

jb15boltPrototype = ItemPrototype('RAY',grabbable=False,stopsBeforeWall=False)
jb15boltPrototype.impactEffect = BlasterBoltImpactEffect(2)

jb15Prototype = ItemPrototype('JB-15 RIFLE',grabbable=True)
jb15Prototype.verbs.append(Shoot(jb15boltPrototype))
